<?php

class Application_Model_Dao_TypeStudy extends Zend_Db_Table_Abstract
{

	protected $_name = 'type_studies';
	protected $_primary = 'id';

	public function init(){
		 
	}
	public function getAll(){
		$result = $this->fetchAll();
		if( !$result )
		throw new Exception("Wystąpił błąd przy pobieraniu danych z bazy. Proszę spróbować jeszcze raz. Jeśli problem będzie się powtarzać, prosimy skontaktować się z administratorem.");
		return $result;
	}
public function getAllTypeStudy(){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('type_studies');
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}
public function getRowTypeStudy($id){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('type_studies')->where('type_studies.id = \''.$id.'\'');
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}
	
	
	public function getTypeStudyByID($id){
		$id = (int)$id;
		$row = $this->fetchRow('id = '.$id);
		if (!$row)
		throw new Exception("Rodzaj studiów o ID $id nie istnieje.");
		return $row->toArray();
	}
	public function getTypeStudyByDesignation($designation){
		$row = $this->fetchRow('designation = '.$designation);
		if (!$row)
		throw new Exception("Rodzaj studiów o nazwie $designation nie istnieje.");
		return $row->toArray();
	}
	public function saveTypeStudy($typeStudy) {
		$data = array(
			'designation' => $typeStudy->getDesignation(),
		);
		$this->insert($data);
		return ;
	}
	public function updateTypeStudy($typeStudy) {
		$data = array(
			'designation' => $typeStudy->getDesignation(),
		);
		$this->update($data, 'id = '. (int)$typeStudy->getId());
		return ;
	}
	public function deleteTypeStudy($typeStudy){
		$this->delete('id =' . (int)$typeStudy->getId());
		return;
	}
	public function getToSelect(){
		$array = $this->getAll()->toArray();
		$typeStudies[''] = "wybierz";
		foreach ($array as $option) {
			$typeStudies[ $option['id'] ] = $option['designation' ] ;
		}
		return $typeStudies;
	}

}
 